package dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import util.Conversion;
import dto.CategoryDTO;
import dto.QuestionDTO;
import dto.QuestionValidDTO;
import entities.Category;
import entities.Question;

/**
 * Session Bean implementation class QuestionDAO
 */
@Stateless
public class QuestionDAO implements QuestionDAORemote {

	@PersistenceContext
	EntityManager em;
	Conversion conv;

	/**
	 * Default constructor.
	 */
	public QuestionDAO() {
		// TODO Auto-generated constructor stub

		conv = new Conversion();
	}

	@Override
	public List<QuestionValidDTO> getAllQuestionsForValidation(int catID) {
		List<QuestionValidDTO> qList = new ArrayList<QuestionValidDTO>();
		TypedQuery<Question> query = em.createQuery(
				"SELECT q FROM Question q WHERE q.category.id = :idCateg ORDER BY q.valid ASC", Question.class);
		query.setParameter("idCateg", catID);
		List<Question> result = query.getResultList();

		for (Question q : result) {
			QuestionValidDTO qDTO = new QuestionValidDTO();
			try {
				qDTO = conv.fromQuestionToQuestionValidDTO(q);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			qList.add(qDTO);
		}

		return qList;
	}
	
	@Override
	public int removeQuestionFromDatabase(QuestionValidDTO qDTO) {
		Query query = em.createQuery("DELETE from Question q WHERE q.id = :id");
		query.setParameter("id", qDTO.getId());
		int deleted = query.executeUpdate();
		return deleted;
	}

	@Override
	public int changeQuestionValid(int qID) {
		System.out.println("QuestionDAO/qID: " + qID);
		Query query = em.createQuery("UPDATE Question q SET q.valid = 1 WHERE q.id = :id");
		query.setParameter("id", qID);
		int update = query.executeUpdate();
		return update;
	}

	public List<QuestionDTO> getAllValidQuestions() {
		TypedQuery<Question> query = em.createQuery("SELECT q FROM Question q WHERE q.valid = 1", Question.class);
		List<Question> resultList = query.getResultList();

		List<QuestionDTO> questions = new ArrayList<QuestionDTO>();

		try {

			for (Question q : resultList) {
				questions.add(conv.fromQuestionToQuestionDTO(q));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return questions;

	}

	public List<QuestionValidDTO> getAllQuestions() {
		List<QuestionValidDTO> qList = new ArrayList<QuestionValidDTO>();
		TypedQuery<Question> query = em.createQuery("SELECT q FROM Question q ORDER BY q.valid DESC", Question.class);
		List<Question> resultList = query.getResultList();

		for (Question q : resultList) {
			QuestionValidDTO qDTO = new QuestionValidDTO();
			qDTO.setId(q.getId());
			qDTO.setText(q.getText());
			qDTO.setAnswer1(q.getAnswer1());
			qDTO.setAnswer2(q.getAnswer2());
			qDTO.setAnswer3(q.getAnswer3());
			qDTO.setAnswer4(q.getAnswer4());
			qList.add(qDTO);
		}
		return qList;
	}

	public List<QuestionDTO> getAllValidQuestionsByCategory(CategoryDTO cat) {
		System.out.println(cat.getId());
		TypedQuery<Question> query = em.createQuery(
				"SELECT q FROM Question q WHERE q.valid = 1 AND q.category.id = :cid", Question.class);
		List<Question> resultList = query.setParameter("cid", cat.getId()).getResultList();

		List<QuestionDTO> questions = new ArrayList<QuestionDTO>();

		try {

			for (Question q : resultList) {
				questions.add(conv.fromQuestionToQuestionDTO(q));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return questions;

	}

	public void addQuestion(QuestionDTO questionDTO, int categID) throws Exception {
		try {
			Question question = conv.fromQuestionDTOToQuestionWithoutCateg(questionDTO);
			TypedQuery<Category> query = em.createQuery("SELECT c FROM Category c WHERE c.id = :idCateg",
					Category.class);
			query.setParameter("idCateg", categID);

			List<Category> result = query.getResultList();
			if (result.size() > 0) {
				question.setCategory(result.get(0));
				List<Question> lq = result.get(0).getQuestions();
				lq.add(question);
				result.get(0).setQuestions(lq);
				for (Question question2 : result.get(0).getQuestions())
					System.out.println(question2.getText());
			}
			if ((em.find(Question.class, question.getId()) != null)) {
				em.merge(question);
			} else {
				em.persist(question);
			}
		} catch (Exception e) {
			throw new Exception("Question could not been added!", e);
		}
	}

}
